


import React from 'react'
import { rolelist, toast } from '../../utils/common';
import { Form, Input, Button, Select } from 'antd';
import { inject, observer } from 'mobx-react';
import { ajax } from '../../api';
import { reg } from '../../utils/validate';

function UserAdd({ user }: any) {
    const { userInfo, changeUserInfo } = user
    const { Option } = Select;
    const [form] = Form.useForm()
    const onFinish = async (value: any) => {
        console.log(value)
        let { code, result }: any = await ajax.register(value)
    }
    const onFinishFailed = () => {
        toast.error("请输入有效数据")
    }
    return (
        <div>
            <h2 style={{ fontSize: '30px' }}>添加管理员</h2>
            <Form
                style={{ background: '#fff', padding: '30px 100px', borderRadius: '10px' }}
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                initialValues={{ password: '123qwe' }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="姓名"
                    name="username"
                    rules={[{ required: true, message: '请输入姓名' }]}
                >
                    <Input placeholder='请输入姓名' />
                </Form.Item>

                <Form.Item
                    label="电话号码"
                    name="phone"
                    rules={[{ required: true, message: '请输入电话号码' },
                    { pattern: reg.phone, message: '请输入有效电话号码' }]}
                >
                    <Input placeholder='请输入电话号码' />
                </Form.Item>

                <Form.Item
                    label="职位"
                    name="role"
                    rules={[{ required: true, message: '请选择职位' }]}
                >
                    <Select placeholder='请选择职位'>
                        {
                            rolelist.map((v, i) => {
                                if (userInfo?.role >= v.role) {
                                    return (
                                        <Option key={i} value={v.value}>{v.text}</Option>
                                    )
                                } else {
                                    return null
                                }
                            })
                        }
                    </Select>
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    rules={[{ required: true, message: '请输入密码' },
                    { pattern: reg.pwd, message: '请输入6-16位数字加字母' }]}
                >
                    <Input disabled placeholder='请输入密码' />
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 8, span: 20 }}>
                    <Button type="primary" htmlType="submit">
                        提交
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default inject("user")(observer(UserAdd))